package com.boilerCloud.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.Menu;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.sys.IHzlqswMenuService;
import com.boilerCloud.util.StringUtil;

/**
 * @类功能说明：菜单交互控制层
 * @类修改者：
 * @修改日期：
 * @修改说明：
 * @公司名称：杭州龙骞科技有限公司
 * @作者：qianfuqiang
 * @创建时间：2013-8-11 下午05:11:04
 * @版本：V1.0
 */
@RestController
public class HzlqswMenuControler {
	@Resource
	private IHzlqswMenuService menuService;
	private static final Logger log = LoggerFactory.getLogger(HzlqswMenuControler.class);
	// 无操作权限返回
	@RequestMapping(value = "/noPermission", produces = "text/plain;charset=UTF-8")
	public ReqResult noPermission() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_no_permission);
		rst.setReturnObject("无操作权限");
		return rst;
	}

	// 获取用户左侧
	@RequestMapping(value = "/getLeftMenu.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public  ReqResult getLeftMenu(HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		
		try {
			// 从session中获取角色ID
			if (request.getSession().getAttribute("usersession") == null) {
				rst.setResultCode(ReqResult.resultCode_not_login);
				rst.setReturnObject("未登录或登录已失效，请先登录");
				System.out.println(rst.getResultCode());
				return rst;
			} else {
				User user = (User)request.getSession().getAttribute("usersession");
				rst.setReturnObject(menuService.getLeftMenu(user.getRoleId()));
				rst.setResultCode(ReqResult.resultCode_Success);
				System.out.println(rst.getResultCode());
				return rst;
			}
			
		} catch (Exception e) {
			log.error("获取用户左侧菜单异常",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}

	// 进入菜单列表页面
	@RequestMapping(value = "/service/menuList.do")
	public ModelAndView menuList() {
		return new ModelAndView("/views/sys/menuList.jsp");
	}

	// 获取用户菜单列表
	@RequestMapping(value = "/service/getMenuList.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getMenuList(HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			Menu menu = new Menu();
			String menuName = request.getParameter("menuName");
			String menuID = request.getParameter("menuID");
			String navMenu = request.getParameter("navMenu");
			if (menuID != null && StringUtil.isNum(menuID)) {
				menu.setID(Integer.parseInt(menuID));
			}
			if (navMenu != null && StringUtil.isNum(navMenu)) {
				menu.setNAVMENU(Integer.parseInt(navMenu));
			}
			menu.setMENUNAME(menuName);
			HzlqswPage<Menu> page = new HzlqswPage<Menu>();
			String yema = request.getParameter("yema");
			String pagelen = request.getParameter("pagelen");
			page.setCurPage(Integer.parseInt(yema));
			page.setPageSize(Integer.parseInt(pagelen));
			if (StringUtil.isNum(yema) && StringUtil.isNum(pagelen)) {
				menuService.getMenuList(0, menu,page);
				rst.setReturnObject(page);
				rst.setResultCode(ReqResult.resultCode_Success);
			} 

		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			log.error("获取用户菜单列表异常",e);
			rst.setReturnObject("系统异常");
		}
		return rst;
	}

	// 预添加菜单
	@RequestMapping(value = "/service/addMenu.do", produces = "text/plain;charset=UTF-8")
	public ReqResult addMenu() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_Success);
		return rst;
	}

	// 添加菜单
	@RequestMapping(value = "/service/doAddMenu.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult doAddMenu(@RequestParam(required = true) String menuName,
			@RequestParam(required = true) String menuUrl, @RequestParam(required = true) String parentID,
			@RequestParam(required = true) String navMenu, @RequestParam(required = false) String urlTarget,
			@RequestParam(required = false) String headImg, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (menuName.isEmpty() || parentID.isEmpty() || navMenu.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
			} else if (!StringUtil.isNum(parentID) || !StringUtil.isNum(navMenu)) {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
			} else {
				User user = (User) request.getSession().getAttribute("usersession");
				Menu menu = new Menu();
				menu.setMENUNAME(menuName);
				menu.setURLTARGET(urlTarget);
				menu.setMENUURL(menuUrl);
				menu.setNAVMENU(Integer.parseInt(navMenu));
				menu.setPARENTID(Integer.parseInt(parentID));
				menu.setHEADIMG(headImg);
				menuService.addMenu(menu, user.getRoleId());
				rst.setResultCode(ReqResult.resultCode_Success);
			}
		} catch (Exception e) {
			log.error("添加菜单异常",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
		}
		return rst;
	}

	// 删除菜单
	@RequestMapping(value = "/service/delMenu.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delMenu(@RequestParam(required = true) String menuID, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (menuID.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
			} else if (!StringUtil.isNum(menuID)) {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
			} else {
				menuService.delMenu(Integer.parseInt(menuID));
				rst.setResultCode(ReqResult.resultCode_Success);
				
			}
			return rst;
		} catch (Exception e) {
			log.error("删除菜单异常",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}
	
	// 预修改菜单
	@RequestMapping(value = "/service/editMenu.do", produces = "text/plain;charset=UTF-8")
	public ReqResult editMenu() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_Success);
		 return rst;
	}

	// 修改菜单
	@RequestMapping(value = "/service/doEditMenu.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult doEditMenu(@RequestParam(required = true) String menuID,
			@RequestParam(required = true) String menuName, @RequestParam(required = true) String menuUrl,
			@RequestParam(required = true) String parentID, @RequestParam(required = true) String navMenu,
			@RequestParam(required = false) String urlTarget, @RequestParam(required = false) String headImg,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			if (menuID.isEmpty() || menuName.isEmpty() || parentID.isEmpty() || navMenu.isEmpty()) {
				rst.setResultCode(ReqResult.resultCode_param_lost);
				rst.setReturnObject("参数缺失");
				return rst;
			} else if (!StringUtil.isNum(menuID) || !StringUtil.isNum(parentID) || !StringUtil.isNum(navMenu)) {
				rst.setResultCode(ReqResult.resultCode_param_error);
				rst.setReturnObject("参数格式错误");
				return rst;
			} else {
				Menu menu = new Menu();
				menu.setID(Integer.parseInt(menuID));
				menu.setMENUNAME(menuName);
				menu.setURLTARGET(urlTarget);
				menu.setMENUURL(menuUrl);
				menu.setNAVMENU(Integer.parseInt(navMenu));
				menu.setPARENTID(Integer.parseInt(parentID));
				menu.setHEADIMG(headImg);
				menuService.updateMenu(menu);
				rst.setResultCode(ReqResult.resultCode_Success);
				return rst;
			}
		} catch (Exception e) {
			log.error("删除修改菜单异常",e);
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("系统异常");
			return rst;
		}
	}
}
